package cn.tedu.pet.passport.controller;


import cn.tedu.pet.commons.consts.HttpConsts;
import cn.tedu.pet.passport.pojo.param.AdminAddNewParam;
import cn.tedu.pet.passport.pojo.param.AdminLoginInfoParam;
import cn.tedu.pet.passport.pojo.param.AdminUpdateInfoParam;
import cn.tedu.pet.passport.pojo.param.AdminUpdatePasswordParam;
import cn.tedu.pet.passport.pojo.vo.AdminListItemVO;
import cn.tedu.pet.passport.pojo.vo.AdminLoginInfoVO2;
import cn.tedu.pet.passport.pojo.vo.AdminStandardVO;
import cn.tedu.pet.passport.pojo.vo.PageData;
import cn.tedu.pet.passport.security.LoginPrincipal;
import cn.tedu.pet.passport.service.IAdminService;
import cn.tedu.pet.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/admin")
@Api(tags = "01. 管理员管理模块")
@Validated
public class AdminController {

   @Autowired
   private  IAdminService adminService;

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam) {
      log.debug("开始处理【管理员登录】的请求，参数：{}", adminLoginInfoParam);
       AdminLoginInfoVO2 adminLoginInfoVO2=adminService.login(adminLoginInfoParam);
       return JsonResult.ok(adminLoginInfoVO2);
    }

    @PostMapping("/logout")
    @ApiOperation("管理员退出登录")
    @ApiOperationSupport(order = 80)
    public JsonResult logout(@RequestHeader(HttpConsts.HEADER_AUTHORIZATION) String jwt) {
        log.debug("开始处理【管理员退出登录】的请求，参数：{}", jwt);
        adminService.logout(jwt);
        return JsonResult.ok();
    }


    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AdminAddNewParam albumAddNewParam) {
        log.debug("开始处理【添加管理员】的请求，参数：{}", albumAddNewParam);
        adminService.addNew(albumAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除管理员")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long"),
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除管理员失败，请提交合法的ID值！")
                                 @RequestParam Long id)throws Exception{
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改管理员")
   @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperationSupport(order = 300)
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "根据ID修改失败，请提交合法的ID值！")
                                             Long id, @Valid AdminUpdateInfoParam adminUpdateInfoParam)throws Exception {
        log.debug("开始处理【修改管理员详情】的业务，参数：{}",adminUpdateInfoParam );
        adminService. updateInfoById(id,adminUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9281/admin/9527/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9281/admin/9527/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用管理员】的请求，参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }



    @GetMapping("/list")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order =420)
    public JsonResult list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal,
                           @Range(min = 1,message = "查询管理员表失败，请提供正确的页面值") Integer page) {
        log.debug("当事人的用户名：{}", loginPrincipal.getUsername());
        log.debug("当事人的ID：{}", loginPrincipal.getId());
        log.debug("开始处理【查询管理员列表】的业务页码：{}",page);
        if(page==null||page<1){
            page=1;
        }
        PageData<AdminListItemVO> pageData= adminService.list(page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询管理员详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long"),
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询管理员详情失败，请提交合法的ID值！")
                                      @RequestParam Long id)throws Exception{
        log.debug("开始处理【根据ID查询管理员详情】的请求，参数：{}", id);
        AdminStandardVO result = adminService.getStandardById(id);
        return JsonResult.ok(result);
    }


    @PostMapping("/updatePassword")
    @ApiOperation("修改管理员密码")
//    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperationSupport(order = 310)
    public JsonResult updatePassword(@Valid AdminUpdatePasswordParam adminUpdatePasswordParam)throws Exception {
        log.debug("开始处理【修改管理员密码】的业务，参数：{}",adminUpdatePasswordParam );
        String username =adminUpdatePasswordParam.getUsername();
        String password = adminUpdatePasswordParam.getPassword();
        adminService.updateInfoByUsername(username,password);
        return JsonResult.ok();
    }
}
